﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;

namespace CodeFirstDemo41
{
    class Program
    {
        static void Main(string[] args)
        {
            var DB = new StepSampleContext();

            var Sta1 = new StateRegion { ID = 1, FullName = "HongKong-SuMaLi", Capital = "Hong Kong" };
            var Sta2 = new StateRegion { ID = 2, FullName = "HoChiMinh-BacKinh", Capital = "Bac Kinh" };
            var Sta3 = new StateRegion { ID = 3, FullName = "HaNoi-NewYork", Capital = "Ha Noi" };

            var Cus1 = new Customer { ID = 1, City = "Thu Duc, tp hcm", FullName = "Tran Thi Van", PhoneNumber = "0923442412", StateRegion = Sta1 };
            var Cus2 = new Customer { ID = 2, City = "Cau Giay, tp ha noi", FullName = "Le Luu Ly", PhoneNumber = "01235453523", StateRegion = Sta2 };
            var Cus3 = new Customer { ID = 3, City = "Binh Tan, tp hcm", FullName = "Lam Tan Luc", PhoneNumber = "0989123813", StateRegion = Sta3 };
            
            var Ord1 = new OrderEntry { ID = 1, OrderDate = DateTime.Now, Total = 23421, Customer = Cus1 };
            var Ord2 = new OrderEntry { ID = 2, OrderDate = DateTime.Now, Total = 87545, Customer = Cus2 };
            var Ord3 = new OrderEntry { ID = 3, OrderDate = DateTime.Now, Total = 98237, Customer = Cus3 };

            DB.StateRegions.Add(Sta1);
            DB.StateRegions.Add(Sta2);
            DB.StateRegions.Add(Sta3);

            DB.Customers.Add(Cus1);
            DB.Customers.Add(Cus2);
            DB.Customers.Add(Cus3);

            DB.OrderEntries.Add(Ord1);
            DB.OrderEntries.Add(Ord2);
            DB.OrderEntries.Add(Ord3);

            //Get Validation
            bool error = false;
            var validationErrors = DB.GetValidationErrors()
                .Where(vr => !vr.IsValid)
                .SelectMany(vr => vr.ValidationErrors);

            foreach (var err in validationErrors)
            {
                Console.WriteLine(err.ErrorMessage);
                error = true;
            }

            if (!error)
            {
                DB.SaveChanges();
                Console.WriteLine("Save sucessful");
            }

        }
    }

    [Table("DatVe")]
    public class OrderEntry
    {
        [Key]
        public long ID { get; set; }

        public System.DateTime OrderDate { get; set; }
        public decimal Total { get; set; }

        public  Customer Customer { get; set; }
    }

    [Table("KhachHang")]
    public class Customer
    {
        [Key]
        public long ID { get; set; }
        [Required]
        [Column("TenKhachHang")]
        public string FullName { get; set; }
        public string City { get; set; }

        [Required]
        [MinLength(10)]
        [MaxLength(11, ErrorMessage = "So Dien Thoai Phai It Hon 12 Chu So")]
        public string PhoneNumber { get; set; }

        public  StateRegion StateRegion { get; set; }
        public virtual ICollection<OrderEntry> OrderEntries { get; set; }
    }

    [Table("ChuyenBay")]
    public class StateRegion
    {
        [Key]
        public long ID { get; set; }
        [Required]
        public string FullName { get; set; }
        public string Capital { get; set; }

        public virtual ICollection<Customer> Customers { get; set; }
    }

    public class StepSampleContext : DbContext
    {
        public StepSampleContext()
            : base("StepSampleContext")
        {
        }

        public DbSet<Customer> Customers { get; set; }
        public DbSet<OrderEntry> OrderEntries { get; set; }
        public DbSet<StateRegion> StateRegions { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //Tu dong Drop va Create neu có bất kỳ sự thay đổi nào trong mô hình dữ liệu
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<StepSampleContext>());
        }

    }
}
